/**
 * @author ccpalu
 */

function getStatements() {
/* It access the Segmented ID data from all patients
 * This data will be stored inthe varibles 'segData' and
 * 'rawDataY'.
 */

	document.body.style.cursor='wait';
	document.getElementById('CNV').ready=false;
	loadingStatus('Accessing the Project Database');

	var s3db = document.getElementById('s3db');

	loadingStatus('Downloading CNV data... <br>It may take a few minutes');	
	doS3qlQuery('statements', '<rule_id>'+s3db.rules.segmentedFile+'</rule_id>','getDataSeg(ans)');
	
	if (document.getElementById('CNV').xAxis == '') {
		doS3qlQuery('statements', '<rule_id>'+ s3db.rules.mapFile+'</rule_id>','getDataMap(ans,"xAxisData")');
	}
	
	return false;
}

function getDataSeg(sid) {
	var s3db = document.getElementById('s3db');
	var cnv = document.getElementById('CNV');

	cnv.dataMap = new Array(sid.length);

	for (var i = 0; i < sid.length; i += 1) {
		if (sid[i].statement_id && sid[i].resource_id) {
			var url = s3db.url + '/download.php?statement_id=' + sid[i].statement_id + '&download=no&key=' + s3db.key;
			
			s3db_jsonpp_call(url, 'segS3db(ans,' + sid[i].resource_id + ',' + i + ')');
		}
	}
	if (document.getElementById('configDiv') == undefined) {
		configOptions();
	}
	else{		
		graphConfigBox();
	}
	return false;
}

function getDataMap(num,variable) {
/* Num - is the statment ID obtained using s3db_jsonpp_call,
 * 		 called in 'getValue' function.
 * Variable -  is a variable where the results will be stored;
 * 
 * It access the file stored in S3DB, identified by 'num'.
 *
 */

	var s3db = document.getElementById('s3db');
	var cnv = document.getElementById('CNV');
	var error=0;
	
	var url = s3db.url+'/download.php?statement_id='+num[0].statement_id+'&download=no&key='+s3db.key;
	
	s3db_jsonpp_call(url, 'rawData(ans)');
			
	return false;
}


function rawData(xAxisData) { 
/* Probe map */
	var cnv = document.getElementById('CNV');
	var ct=-1;
	var currentChr='1';
	var rawChr=new Array;
	var rawChrm=new Array;
	var typeChr='';
	rawChr=xAxisData.split(/%0A/g);
	rawChr=rawChr.slice(1,rawChr.length-1);
	
	for (var x in rawChr ){
		rawChr[x] = rawChr[x].split(/%09/g);
		typeChr = rawChr[x][1];
		
		if (typeChr == currentChr) 
			ct = ct + 1;
		else 
			ct = 0;
		
		if (!rawChrm[typeChr]) 
			rawChrm[typeChr] = new Array;
		
		if (!rawChrm[typeChr][ct]) 
			rawChrm[typeChr][ct] = new Array;
			
			
			rawChrm[typeChr][ct]=rawChr[x][2]*1;
			
			currentChr=typeChr;
		
	}
	
	cnv.xAxis=rawChrm;
	addUserSample();

	return  false;
}

function addUserSample(){
	
	var cnv = document.getElementById('CNV');
	
	if (cnv.segData[9]!=undefined) {
		if ((cnv.dataMap.length == cnv.segData[9].length) && (!cnv.ready)) {
			organizeOptions();
			cnv.chRegions=chromossomeMetrics();
			createChList();
			createExportForm();
			document.getElementById('s3db').done=false;
			otherOptionMenuDisplay();
		}
	}
	return false;
}

function organizeOptions(){
	var cnv = document.getElementById('CNV');

	var opt=document.getElementById("choose_sample").options;
	
	for (var i=opt.length-cnv.dataMap.length;i<opt.length;i=i+1){
		opt[i].value=findInArray(cnv.dataMap,opt[i].value);
	}
	document.getElementById("choose_sample").disabled=false;

	remove_element_by_id('LoadingStatus');
	document.body.style.cursor='auto';
	cnv.ready=true;
	plotData();
	
	return false;
}

function chromossomeMetrics(){
	var cnv = document.getElementById('CNV');
	var chmetrics=new Array();
	for (var i=1; i < cnv.segData.length; i=i+1){
		chmetrics[i]=cnv.max[i]-cnv.min[i];
	}
	
	chmetrics.total=function(){
		var lengthSum = 0;
		for(var i=0;i<cnv.ch.length;i=i+1){
			lengthSum=lengthSum+chmetrics[cnv.ch[i]];
		}
		return lengthSum;
	};
	return chmetrics;
}

function plotData(){
	/* After uploading the data, a overview of data is plotted.*/
		
		//document.getElementById('sim_method').selectedIndex=1;
		//document.getElementById('choose_sample').selectedIndex=1;

		
		if(chSelected().length==0){ //April 2011
			document.getElementById('chAll').checked = true;
			var chList = new Array();
			var j=0;
			for (var i=1;i<=22;i=i+1){
				if (!document.getElementById('ch'+i).disabled){
					chList[j] = i;
					j=j+1;
					document.getElementById('ch'+i).checked = true;
				}
			}
		
			document.getElementById('CNV').ch = chList;
		}
		document.getElementById("submitParametersButton").disabled=false;
		//document.getElementById('submitParametersButton').click();
		similarity();
		
		return false;
	}
